// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FloorFilterTestHelper.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Runtime
{
    using System;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Vision.Cameras;
    using Microsoft.Robotics.Vision.Runtime.Cameras;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Helper class to host some shared test methods
    /// </summary>
    public class FloorFilterTestHelper 
    {
        /// <summary>
        /// Synthesizes Depth Frame With Floor
        /// </summary>
        /// <param name="width">Frame width</param>
        /// <param name="height">Frame height</param>
        /// <param name="floorStartRow">Floor start row</param>
        /// <param name="horizonRow">This is where horizon is</param>
        /// <param name="elevationFactorB">B constant</param>
        /// <returns>Depth frame and floor pixel count</returns>
        public static Tuple<short[], int> SynthesizeDepthFrameWithFloor(int width, int height, int floorStartRow, int horizonRow, int elevationFactorB)
        {
            int markedCount = 0;

            short[] depthFrame = new short[width * height];

            int index = 0;

            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    if (i > horizonRow && i > floorStartRow)
                    {
                        depthFrame[index] = (short)(elevationFactorB / (i - horizonRow));

                        ++markedCount;
                    }
                    else
                    {
                        // let the rest of the frame be a flat wall 
                        depthFrame[index] = 3000;
                    }

                    ++index;
                }
            }

            return new Tuple<short[], int>(depthFrame, markedCount);
        }

        /// <summary>
        /// Verifies floor markup
        /// </summary>
        /// <param name="depthFrame">Depth frame</param>
        /// <param name="width">Frame width</param>
        /// <param name="height">Frame height</param>
        /// <param name="floorStartRow">Floor start row</param>
        /// <param name="horizonRow">This is where horizon is</param>
        /// <param name="elevationFactorB">B constant</param>
        /// <returns>Marked pixels</returns>
        public static int CountMarkedFloorPixels(short[] depthFrame, int width, int height, int floorStartRow, int horizonRow, int elevationFactorB)
        {
            int index = 0;

            int markedCount = 0;

            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    short depth = depthFrame[index];

                    if (i > horizonRow && i > floorStartRow)
                    {
                        ++markedCount;
                    }

                    ++index;
                }
            }

            return markedCount;
        }

        /// <summary>
        /// Creates a realistic 8 strip calibration data for the floor filter
        /// </summary>
        /// <param name="height">Frame Height</param>
        /// <param name="width">Frame width</param>        
        /// <returns>Calibration data</returns>
        public static double[][] GetRealisticCalibrationData(int height, int width)
        {
            int nominalA = height / 2;
            int nominalB = 1350 * (height - nominalA);

            return new double[3][]
            {
                new double[] { nominalA + 1, nominalA + 3, nominalA - 1, nominalA + 3, nominalA - 1, nominalA - 2, nominalA + 1, nominalA + 2 },
                new double[] { nominalB - 9, nominalB - 13, nominalB + 11, nominalB + 13, nominalB - 16, nominalB + 21, nominalB + 6, nominalB - 22 },
                new double[] { 1354,   1355,   1353,   1356,   1333,   1331,   1334,    1332 }
            };
        }

        /// <summary>
        /// Creates a simple 4 strip calibration data for the floor filter
        /// </summary>
        /// <returns>Calibration data</returns>
        public static double[][] GetPerfectCalibrationData()
        {
            return new double[3][]
            {
                new double[] { 240, 240, 240, 240 },
                new double[] { 300000, 300000, 300000, 300000 },
                new double[] { 1350, 1350, 1350, 1350 }
            };
        }
    }
}
